package com.xuyixuan.do_jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class ControllerUser {

    @Autowired
    private ServiceUser userService;

    @PostMapping
    public int addUser(@RequestBody EntityUser user) {
        return userService.addUser(user);
    }

    @GetMapping("/{id}")
    public EntityUser getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }

    @PutMapping("/{id}")
    public int updateUser(@PathVariable int id, @RequestBody EntityUser user) {
        user.setId(id);
        return userService.updateUser(user);
    }

    @DeleteMapping("/{id}")
    public int deleteUser(@PathVariable int id) {
        return userService.deleteUser(id);
    }

    @GetMapping
    public List<EntityUser> getAllUsers() {
        return userService.getAllUsers();
    }
}